package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.domain.Travels;
import cn.leyaber.hrm.domain.Wholename;
import cn.leyaber.hrm.query.TravelsQuery;
import cn.leyaber.hrm.service.ITravelsService;
import cn.leyaber.hrm.service.IWholenameService;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import cn.leyaber.hrm.vo.TravelsVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/travels")
public class TravelsController {
    @Autowired
    public ITravelsService travelsService;
    @Autowired
    private IWholenameService wholenameService;

    /**
     * 批量删除
     */
    @PatchMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long>ids){
        try {
            travelsService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改失败！"+e.getMessage());
        }
    }
    /**
     * 修改状态
     */
    @PostMapping("/Modify_status")
    public AjaxResult Modify_status(@RequestBody Travels travels){
        try {
            travelsService.Modify_status(travels);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改状态败！"+e.getMessage());
        }
    }

    /**
    * 保存和修改公用的
    * @param travels  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Travels travels){
        try {
            Travels travels1 = new Travels();
            long time = new Date().getTime();
             travels1.setTime(time);
            travelsService.insert(travels1);
            if(travels.getId()!=null){
                travelsService.updateById(travels);
            }else{
                travelsService.insert(travels);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            travelsService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Travels get(@PathVariable("id")Long id)
    {
        return travelsService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
//    @RequestMapping(value = "/list",method = RequestMethod.GET)
//    public List<TravelsVo> list(){
//        List<Travels> travels = travelsService.selectList(null);
//        ArrayList<TravelsVo> travelsVos = new ArrayList<>();
//        for (Travels travel : travels) {
//            TravelsVo travelsVo = JSON.parseObject(JSON.toJSONString(travel), TravelsVo.class);
//            if (StringUtils.isEmpty(travel.getUserWholenameId())){
//                 travelsVos.add(travelsVo);
//                 continue;
//            }
//            Wholename wholename = wholenameService.selectById(travel.getUserWholenameId());
//              travelsVo.setUser(wholename.getUser());
//            travelsVos.add(travelsVo);
//        }
//        return travelsVos;
//    }
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<TravelsVo> selectTravelsVo() {

        return travelsService.selectTravelsVo();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<TravelsVo> json(@RequestBody TravelsQuery query)
    {
        Page<Travels> travelsPage1 = new Page<>(query.getPage(), query.getRows());
        Page<Travels> travelsPage = travelsService.selectPage(travelsPage1);
        Page<TravelsVo> page1 = JSON.parseObject(JSON.toJSONString(travelsPage), Page.class);
        ArrayList<TravelsVo> objects = new ArrayList<>();
        for (Travels record : travelsPage.getRecords()) {
            TravelsVo travelsVo = JSON.parseObject(JSON.toJSONString(record), TravelsVo.class);
            if (StringUtils.isEmpty(record.getUserWholenameId())) {
                objects.add(travelsVo);
                continue;
            }
            Wholename wholename = wholenameService.selectById(record.getUserWholenameId());
            travelsVo.setUser(wholename.getUser());
            objects.add(travelsVo);
         }
        page1.setRecords(objects);
        return new PageList<TravelsVo>(page1.getTotal(),page1.getRecords());
        }
    }

